#!/bin/bash 
fun_badvpn() {
clear
	echo -e "\033[0;34m┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\033[0m"
	echo -e "\033[0;34m┃\E[44;1;37m            GERENCIAR BADVPN             \E[0m\033[0;34m┃"
	echo -e "\033[0;34m┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\033[0m\n"
    if ps x | grep -w udpvpn | grep -v grep 1>/dev/null 2>/dev/null; then
        echo -e "\033[1;33mPORTAS\033[1;37m: \033[1;32m$(netstat -nplt | grep 'badvpn-ud' | awk {'print $4'} | cut -d: -f2 | xargs)"
    else
        sleep 0.1
    fi
    var_sks1=$(ps x | grep "udpvpn"|grep -v grep > /dev/null && echo -e "\033[1;32m♦ " || echo -e "\033[1;31m○ ")
    echo ""
    echo -e "\E[38;5;196m|\E[38;5;33m01\E[38;5;196m|\033[1;37m➜ \033[1;33mATIVAR/DESATIVAR BADVPN(PADRÃO 7300) $var_sks1 \033[0m"
	echo -e "\E[38;5;196m|\E[38;5;33m02\E[38;5;196m|\033[1;37m➜ \033[1;33mATIVAR AS PORTAS BADVPN (7100-7900)\033[0m"
    echo -e "\E[38;5;196m|\E[38;5;33m03\E[38;5;196m|\033[1;37m➜ \033[1;33mABRIR PORTA\033[0m"
    echo -e "\E[38;5;196m|\E[38;5;33m00\E[38;5;196m|\033[1;37m➜ \033[1;33mVOLTAR\033[0m"
    echo ""
    echo -ne "\033[1;32mO QUE DESEJA FAZER \033[1;33m?\033[1;37m "
    read resposta
    if [[ "$resposta" = '1' ]]; then
        if ps x | grep -w udpvpn | grep -v grep 1>/dev/null 2>/dev/null; then
            clear
            fun_stopbad () {
                for pidudpvpn in $(screen -ls | grep ".udpvpn" | awk {'print $1'}); do
                    screen -r -S "$pidudpvpn" -X quit
				done
                [[ $(grep -wc "udpvpn" /etc/autostart) != '0' ]] && {
                    sed -i '/udpvpn/d' /etc/autostart
                }
                sleep 1
                screen -wipe >/dev/null
            }
			echo -e "\033[0;34m┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\033[0m"
			echo -e "\033[0;34m┃\E[44;1;37m            GERENCIAR BADVPN             \E[0m\033[0;34m┃"
			echo -e "\033[0;34m┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\033[0m\n"
            echo -e "\033[1;32mDESATIVANDO O BADVPN\033[1;33m"
            fun_stopbad
            echo ""
            echo -e "\033[1;32mBADVPN DESATIVADO COM SUCESSO!\033[1;33m"
            sleep 0.5
            fun_badvpn
			else
            fun_udpon () {
                screen -dmS udpvpn /bin/badvpn-udpgw --listen-addr 127.0.0.1:7300 --max-clients 10000 --max-connections-for-client 8
                [[ $(grep -wc "udpvpn" /etc/autostart) = '0' ]] && {
                    echo -e "ps x | grep 'udpvpn' | grep -v 'grep' || screen -dmS udpvpn /bin/badvpn-udpgw --listen-addr 127.0.0.1:7300 --max-clients 10000 --max-connections-for-client 8 --client-socket-sndbuf 10000" >> /etc/autostart
                } || {
                    sed -i '/udpvpn/d' /etc/autostart
                    echo -e "ps x | grep 'udpvpn' | grep -v 'grep' || screen -dmS udpvpn /bin/badvpn-udpgw --listen-addr 127.0.0.1:7300 --max-clients 10000 --max-connections-for-client 8 --client-socket-sndbuf 10000" >> /etc/autostart
                }
                sleep 1
            }
			clear
            inst_udp () {
                cd $HOME
                wget https://raw.githubusercontent.com/modderajuda/donomodderajuda/main/I/badvpn-udpgw -o /dev/null
                mv -f $HOME/badvpn-udpgw /bin/badvpn-udpgw
                chmod 777 /bin/badvpn-udpgw
            }
			echo -e "\033[0;34m┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\033[0m"
			echo -e "\033[0;34m┃\E[44;1;37m            GERENCIAR BADVPN             \E[0m\033[0;34m┃"
			echo -e "\033[0;34m┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\033[0m\n"
			echo -e "\033[1;32mINICIANDO O BADVPN... \033[1;33m"
            inst_udp
			echo ""
			echo -e "\033[1;32mBADVPN ATIVADO COM SUCESSO\033[1;33m"
			sleep 0.5
            fun_udpon
            sleep 0.5
            fun_badvpn
        fi


		    elif [[ "$resposta" = '2' ]]; then
        if ps x | grep -w udpvpn | grep -v grep 1>/dev/null 2>/dev/null; then
            clear
	echo -e "\033[0;34m┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\033[0m"
	echo -e "\033[0;34m┃\E[44;1;37m            GERENCIAR BADVPN             \E[0m\033[0;34m┃"
	echo -e "\033[0;34m┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\033[0m\n"
    echo -e "\033[1;32mINICIANDO TODAS AS PORTAS BADVPN... \033[0m"
                sleep 0.5
    echo -e ""
	echo -e "\033[1;32mPORTAS FECHAM AO REINICIAR A MAQUINA	\033[0m"
				sleep 1.5
            screen -dmS udpvpn /bin/badvpn-udpgw --listen-addr 127.0.0.1:7100 --max-clients 9000 --max-connections-for-client 8 --client-socket-sndbuf 10000 --udp-mtu 9000

			screen -dmS udpvpn /bin/badvpn-udpgw --listen-addr 127.0.0.1:7200 --max-clients 9000 --max-connections-for-client 8 --client-socket-sndbuf 10000 --udp-mtu 9000

			screen -dmS udpvpn /bin/badvpn-udpgw --listen-addr 127.0.0.1:7300 --max-clients 9000 --max-connections-for-client 8 --client-socket-sndbuf 10000 --udp-mtu 9000

			screen -dmS udpvpn /bin/badvpn-udpgw --listen-addr 127.0.0.1:7400 --max-clients 9000 --max-connections-for-client 8 --client-socket-sndbuf 10000 --udp-mtu 9000
	
			screen -dmS udpvpn /bin/badvpn-udpgw --listen-addr 127.0.0.1:7500 --max-clients 9000 --max-connections-for-client 8 --client-socket-sndbuf 10000 --udp-mtu 9000
	
			screen -dmS udpvpn /bin/badvpn-udpgw --listen-addr 127.0.0.1:7600 --max-clients 9000 --max-connections-for-client 8 --client-socket-sndbuf 10000 --udp-mtu 9000

			screen -dmS udpvpn /bin/badvpn-udpgw --listen-addr 127.0.0.1:7700 --max-clients 9000 --max-connections-for-client 8 --client-socket-sndbuf 10000 --udp-mtu 9000

			screen -dmS udpvpn /bin/badvpn-udpgw --listen-addr 127.0.0.1:7800 --max-clients 9000 --max-connections-for-client 8 --client-socket-sndbuf 10000 --udp-mtu 9000

			screen -dmS udpvpn /bin/badvpn-udpgw --listen-addr 127.0.0.1:7900 --max-clients 9000 --max-connections-for-client 8 --client-socket-sndbuf 10000 --udp-mtu 9000
			echo -e ""
            echo -e "\033[1;32mPORTAS BADVPN ATIVADAS... \033[0m\n"
			sleep 0.5
            sleep 0.5
            fun_badvpn
			fi


    elif [[ "$resposta" = '3' ]]; then
        if ps x | grep -w udpvpn | grep -v grep 1>/dev/null 2>/dev/null; then
            clear
	echo -e "\033[0;34m┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\033[0m"
	echo -e "\033[0;34m┃\E[44;1;37m            GERENCIAR BADVPN             \E[0m\033[0;34m┃"
	echo -e "\033[0;34m┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\033[0m\n"
            echo -ne "\033[1;32mQUAL PORTA DESEJA ULTILIZAR \033[1;33m?\033[1;37m: "
            read porta
            [[ -z "$porta" ]] && {
                echo ""
                echo -e "\033[1;31mPorta invalida!"
                sleep 0.5
                clear
                fun_badvpn
            }
            fun_abrirptbad() {
                screen -dmS udpvpn /bin/badvpn-udpgw --listen-addr 127.0.0.1:$porta --max-clients 10000 --max-connections-for-client 8
                sleep 1
            }
			echo ""
            echo -e "\033[1;32mINICIANDO O BADVPN NA PORTA \033[1;31m$porta\033[1;33m\n"
			sleep 0.5
			echo -e "\033[1;32mPORTA \033[1;31m$porta\033[1;32m ATIVADO COM SUCESSO\033[1;33m"
            fun_abrirptbad
            sleep 0.5
            fun_badvpn
			else
            echo -e "\033[1;31mFUNCAO INDISPONIVEL\n\n\033[1;33mATIVE O BADVPN PRIMEIRO !\033[1;33m"
            sleep 0.5
            fun_badvpn
        fi
    elif [[ "$resposta" = '00' ]]; then
        echo ""
        echo -e "\033[1;31mRetornando...\033[0m"
        sleep 1
        menu
    else
        echo ""
        echo -e "\033[1;31mOpcao invalida !\033[0m"
        sleep 1
        fun_badvpn
    fi
}
clear
fun_badvpn
